home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / CBINSERT.C < prev    next >
Text File  |  1991-09-23  |  4KB  |  172 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbinsert.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDDEF
  11. #include <stddef.h>
  12. #endif
  13. #ifdef AC_STDLIB
  14. #include <stdlib.h>
  15. #endif
  16. #ifdef AC_STRING
  17. #include <string.h>
  18. #endif
  19.  
  20. /* library headers */
  21. #include <blkio.h>
  22. #include <lseq.h>
  23.  
  24. /* local headers */
  25. #include "cbase_.h"
  26.  
  27. /*man---------------------------------------------------------------------------
  28. NAME
  29.      cbinsert - insert record
  30.  
  31. SYNOPSIS
  32.      #include <cbase.h>
  33.  
  34.      int cbinsert(cbp, buf)
  35.      cbase_t *cbp;
  36.      const void *buf;
  37.  
  38. DESCRIPTION
  39.      The cbinsert function inserts the record pointed to by buf into
  40.      cbase cbp.  The record is inserted after the current record.  The
  41.      record cursor and all the key cursors are set to the inserted
  42.      record.
  43.  
  44.      cbinsert will fail if one or more of the following is true:
  45.  
  46.      [EINVAL]       cbp is not a valid cbase pointer.
  47.      [EINVAL]       buf is the NULL pointer.
  48.      [CBEDUP]       A field in the record pointed to by buf contains
  49.                     an illegal duplicate key.
  50.      [CBELOCK]      cbp is not write locked.
  51.      [CBENOPEN]     cbp is not open.
  52.  
  53. SEE ALSO
  54.      cbdelcur, cbrcursor.
  55.  
  56. DIAGNOSTICS
  57.      Upon successful completion, a value of 0 is returned.  Otherwise,
  58.      a value of -1 is returned, and errno set to indicate the error.
  59.  
  60. ------------------------------------------------------------------------------*/
  61. #ifdef AC_PROTO
  62. int cbinsert(cbase_t *cbp, const void *buf)
  63. #else
  64. int cbinsert(cbp, buf)
  65. cbase_t *cbp;
  66. const void *buf;
  67. #endif
  68. {
  69.     void *        buf2    = NULL;
  70.     cbrpos_t    cbrpos    = NIL;
  71.     int        found    = 0;
  72.     int        i    = 0;
  73.     lspos_t        lspos    = NIL;
  74.  
  75.     /* validate arguments */
  76.     if (!cb_valid(cbp) || buf == NULL) {
  77.         errno = EINVAL;
  78.         return -1;
  79.     }
  80.  
  81.     /* check if not open */
  82.     if (!(cbp->flags & CBOPEN)) {
  83.         errno = CBENOPEN;
  84.         return -1;
  85.     }
  86.  
  87.     /* check if not write locked */
  88.     if (!(cbp->flags & CBWRLCK)) {
  89.         errno = CBELOCK;
  90.         return -1;
  91.     }
  92.  
  93.     /* check for illegal duplicate keys */
  94.     if (cbgetrcur(cbp, &cbrpos) == -1) {    /* save current record pos */
  95.         CBEPRINT;
  96.         return -1;
  97.     }
  98.     for (i = 0; i < cbp->fldc; ++i) {
  99.         if (cbp->fldv[i].flags & CB_FKEY) {
  100.             if (cbp->fldv[i].flags & CB_FUNIQ) {
  101.                 buf2 = calloc((size_t)1, cbp->fldv[i].len);
  102.                 if (buf2 == NULL) {
  103.                     CBEPRINT;
  104.                     errno = ENOMEM;
  105.                     return -1;
  106.                 }
  107.                 memcpy(buf2, (char *)buf + cbp->fldv[i].offset, cbp->fldv[i].len);
  108.                 found = cbkeysrch(cbp, i, buf2);
  109.                 if (found == -1) {
  110.                     CBEPRINT;
  111.                     free(buf2);
  112.                     return -1;
  113.                 }
  114.                 if (found == 1) {
  115.                     free(buf2);
  116.                     errno = CBEDUP;
  117.                     return -1;
  118.                 }
  119.                 free(buf2);
  120.                 buf2 = NULL;
  121.             }
  122.         }
  123.     }
  124.     if (cbsetrcur(cbp, &cbrpos) == -1) {    /* restore record pos */
  125.         CBEPRINT;
  126.         return -1;
  127.     }
  128.  
  129.     /* insert record */
  130.     if (lsinsert(cbp->lsp, buf) == -1) {
  131.         CBEPRINT;
  132.         return -1;
  133.     }
  134.  
  135.     /* get position of inserted record */
  136.     if (lsgetcur(cbp->lsp, &lspos) == -1) {
  137.         CBEPRINT;
  138.         return -1;
  139.     }
  140.     cbrpos = lspos;
  141.  
  142.     /* insert keys */
  143.     for (i = 0; i < cbp->fldc; ++i) {
  144.         if (cbp->fldv[i].flags & CB_FKEY) {
  145.             /* construct (key, record position) pair */
  146.             if (btkeysize(cbp->btpv[i]) != (cbp->fldv[i].len + sizeof(cbrpos_t))) {
  147.                 CBEPRINT;
  148.                 errno = CBEPANIC;
  149.                 return -1;
  150.             }
  151.             buf2 = calloc((size_t)1, cbp->fldv[i].len + sizeof(cbrpos_t));
  152.             if (buf2 == NULL) {
  153.                 CBEPRINT;
  154.                 errno = ENOMEM;
  155.                 return -1;
  156.             }
  157.             memcpy(buf2, (char *)buf + cbp->fldv[i].offset, cbp->fldv[i].len);
  158.             memcpy((char *)buf2 + cbp->fldv[i].len, &cbrpos, sizeof(cbrpos_t));
  159.             /* insert key */
  160.             if (btinsert(cbp->btpv[i], buf2) == -1) {
  161.                 CBEPRINT;
  162.                 free(buf2);
  163.                 return -1;
  164.             }
  165.             free(buf2);
  166.             buf2 = NULL;
  167.         }
  168.     }
  169.  
  170.     return 0;
  171. }
  172.